package com.hxzy.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Desc 描述
 * @Author: admin
 * @Date: 2023/08/21/11:51
 */
@EnableOpenApi
@Configuration
public class Swagger3Config {


    @Bean
    public Docket createRestApi(){
        Docket docket= new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                ;


        //添加一个请求头，key=Authorization，这个根据你实际情况来改名称
        docket.securitySchemes(Arrays.asList(new ApiKey("Authorization", "Authorization", "header")));

        AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] scopes = {scope};
        SecurityReference reference = new SecurityReference("Authorization", scopes);
        List refList = new ArrayList();
        refList.add(reference);
        SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
        List cxtList = new ArrayList();
        cxtList.add(context);
        docket.securityContexts(cxtList);

        return docket;
    }


    /**
     * 说明文档
     * @return
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("接口文档说明")
                .description("指向的网站")
                .version("1.1")
                .build();
    }
}
